package com.example.administrator.newxlmedical;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;

import com.example.administrator.baseproject.BaseActivity;

import java.util.ArrayList;

import adapters.BitmapActivityPagerAdapter;
import beans.NewUserInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.ActivityUtil;
import utils.ObjectSaveUtil;


public class BitmapActivity extends BaseActivity {

    @BindView(R.id.vp_bitmap_activity)
    ViewPager viewPager;

    private ArrayList<String> imagePathList;
    private boolean isMove;

    private SharedPreferences sharedPreferences;
    private int timeout = 60000;
    private NewUserInfoBean.DataBean userInfoBean;

    private MyHandler allHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        super.initView();
        imagePathList = getIntent().getStringArrayListExtra(Constants.IMAGE_PATH);
        int position = getIntent().getIntExtra(Constants.IMAGE_POSITION, 0);

        allHandler = SingletonClass.getSingleton(MyHandler.class);
        userInfoBean = (NewUserInfoBean.DataBean) ObjectSaveUtil.readObject(BitmapActivity.this, Constants.USER_INFO_BEAN, Constants.USER_INFO_BEAN);

        BitmapActivityPagerAdapter pagerAdapter = new BitmapActivityPagerAdapter(imagePathList, BitmapActivity.this);
        viewPager.setAdapter(pagerAdapter);

        viewPager.setCurrentItem(position);

        initSharedPreferences();
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                allHandler.sendEmptyMessageDelayed(HandlerConstant.NO_OPERATION, timeout);
                if (!isMove) {
                    finish();
                }
                isMove=false;
                break;
            case MotionEvent.ACTION_MOVE:
                isMove = true;
                break;
            case MotionEvent.ACTION_DOWN:
                allHandler.removeMessages(HandlerConstant.NO_OPERATION);
                break;
        }
        return super.dispatchTouchEvent(event);
    }
    private void initSharedPreferences() {
        sharedPreferences = ActivityUtil.getSharedPreferences(BitmapActivity.this);
        if (sharedPreferences != null) {
            timeout = Integer.valueOf(sharedPreferences.getString(Constants.LOCK_SCREEN + userInfoBean.getID(), "60")) * 1000;
        }
    }
}
